/**
 * 
 */
package template.page.admin;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.click.control.FieldSet;
import org.apache.click.control.Form;
import org.apache.click.control.HiddenField;
import org.apache.click.control.Option;
import org.apache.click.control.Select;
import org.apache.click.control.Submit;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.extras.control.NumberField;
import org.apache.click.util.Bindable;

import template.page.BorderPage;
import cvcg.model.Game;
import cvcg.model.Player;
import cvcg.model.Score;
import cvcg.util.GameComparator;

/**
 * @author Penetrator
 *
 */
@SuppressWarnings("serial")
public class EditScore extends BorderPage {
	
	public String title = "Edit Score";
	private Form scoreForm = new Form("scoreForm");
	private Select playerSelect = new Select("playerSelect");
	private Select gameSelect = new Select("gameSelect");
	private HiddenField idField = new HiddenField("id",Long.class);
	
	@Bindable protected Long id;
	
	public EditScore() 
	{
		addControl(scoreForm);		
		
        FieldSet detailFieldSet = new FieldSet("Details");
        scoreForm.add(detailFieldSet);
        scoreForm.add(idField);

        detailFieldSet.add(playerSelect);
        detailFieldSet.add(gameSelect);
        detailFieldSet.add(new NumberField("points", true));
                      
        scoreForm.add(new Submit("ok", " OK ", this, "onOkClicked"));
        scoreForm.add(new Submit("cancel", this, "onCancelClicked"));
	}
	
    /* (non-Javadoc)
	 * @see org.apache.click.Page#onInit()
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public void onInit() 
	{
		playerSelect.setDataProvider(new DataProvider() 
		{            			
			public List getData() 
            {
                List<Option> optionList = new ArrayList<Option>();
                List<Player> players = gameSessionService.getAllPlayers();
                for (Player player : players) 
                {
                	optionList.add(new Option(player.getId(), player.getName()));
				}
                return optionList;
            }
        });
                       
		gameSelect.setDataProvider(new DataProvider() 
		{            			
			public List getData() 
            {
                List<Option> optionList = new ArrayList<Option>();
                List<Game> games = gameSessionService.getAllGamesWithoutScore();
                Score currentScore = gameSessionService.getScoreById(id);
                if (currentScore!=null) 
                {
                	games.add(currentScore.getGame());
                }
                Collections.sort(games,new GameComparator());

                for (Game game : games) 
                {
                	optionList.add(new Option(game.getId(), game.getName()));
				}
                return optionList;
            }
        });

		super.onInit();
	}

	@Override
    public void onGet() {
        if (id != null) {
            Score score = gameSessionService.getScoreById(id);

            if (score != null) 
            {
            	scoreForm.copyFrom(score);              	
            	gameSelect.setValue(score.getGameId().toString());
            	playerSelect.setValue(score.getPlayerId().toString());            	
            }
        }
    }

	/**
     * Handle the OK button click event.
     *
     * @return true
     */
    public boolean onOkClicked() {
        if (scoreForm.isValid()) 
        {
        	Long idValue = (Long) idField.getValueObject();
        	gameSessionService.saveScore(idValue, scoreForm,playerSelect,gameSelect);
        	setRedirect(ManageScores.class);
        	return true;
        }
        
        return false;
    }

    /**
     * Handle the Cancel button click event.
     *
     * @return false
     */
    public boolean onCancelClicked() {
    	setRedirect(ManageScores.class);
        return true;
    }
}
